package com.yrmao.chiefenergyofficer.ui.common;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yrmao.chiefenergyofficer.R;
import com.yrmao.chiefenergyofficer.utils.DisplayUtil;

/**
 * Created by yemao on 16/11/14.
 */

public class ToolBarUI extends BaseActivity {
    private FrameLayout content_frame;
    protected Toolbar mToolbar;
    private TextView tv_title;
    private Button btn_right, btn_left;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.ac_bar);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //设置状态栏4.4以后可使用
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.white));
                }
            }
        }
        initView();

    }

    private void initView() {
        content_frame = (FrameLayout) findViewById(R.id.content_frame);
        mToolbar = (Toolbar) findViewById(R.id.toolbar_base);
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_right = (Button) findViewById(R.id.btn_right);
        btn_left = (Button) findViewById(R.id.btn_left);
    }

    /**
     * 设置toolbar的样样式
     * 白色背景黑色字体
     * 其它颜色背景白色字体
     */
    protected void setStyleColor(int color) {

        mToolbar.setBackgroundColor(color);
        if (color == getResources().getColor(R.color.white)) {
            mToolbar.setTitleTextColor(getResources().getColor(R.color.black));
            mToolbar.setSubtitleTextColor(getResources().getColor(R.color.black));
        } else {
            mToolbar.setTitleTextColor(getResources().getColor(R.color.white));
            mToolbar.setSubtitleTextColor(getResources().getColor(R.color.white));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //设置状态栏4.4以后可使用
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(color);
                }
            }
        }

    }

    protected void setLiftBotton(String value, Integer icon, View.OnClickListener onclick) {
        if (value != null) {
            btn_left.setText(value);
        }
        if (icon != null) {
            Drawable drawable = getResources().getDrawable(icon.intValue());
            btn_left.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            ViewGroup.LayoutParams lp = btn_left.getLayoutParams();
            btn_left.setLayoutParams(lp);
        }
        btn_left.setOnClickListener(onclick);
    }

    protected void setLiftBack() {
        btn_left.setText(getString(R.string.back));
        Drawable drawable = getResources().getDrawable(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
        btn_left.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        ViewGroup.LayoutParams lp = btn_left.getLayoutParams();
        btn_left.setLayoutParams(lp);
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    protected void initToolBarMenu() {
        mToolbar.inflateMenu(R.menu.toolbar_menu);
    }

    protected void setRightToolBarBtn(String value, Integer icon, View.OnClickListener onclick) {
        if (value != null) {
            btn_right.setText(value);
        }
        if (icon != null) {
            btn_right.setBackgroundResource(icon.intValue());
            ViewGroup.LayoutParams lp = btn_right.getLayoutParams();
            lp.width = DisplayUtil.dip2px(this, 30);
            lp.height = DisplayUtil.dip2px(this, 30);
            btn_right.setLayoutParams(lp);
        }
        btn_right.setOnClickListener(onclick);
    }

    protected void setLiftToolBarBack() {
        mToolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    protected void setToolBarTitle(String title) {
        tv_title.setText(title);
    }

    protected void setToolBarTitle(int titleId) {
        tv_title.setText(getString(titleId));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_item_one:
                break;
            case R.id.action_item_two:
                break;
        }
        return true;
    }

    @Override
    public void setContentView(int layoutResID) {
        getLayoutInflater().inflate(layoutResID, content_frame);
    }


}
